package kr.hkit.summary_java.serviceui;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;

public class Panel_JTable extends JPanel implements ActionListener {
	private JTable table;
	private JButton btnPrn;

	/**
	 * Create the panel.
	 */
	public Panel_JTable() {

		initialize();
		tableCellAlginAndWith();
	}

	private void tableCellAlginAndWith() {
		//가운데정렬
		DefaultTableCellRenderer dtcrCenter = new DefaultTableCellRenderer();
		dtcrCenter.setHorizontalAlignment(SwingConstants.CENTER);
		
		//우측정렬
		DefaultTableCellRenderer dtcrRight = new DefaultTableCellRenderer();
		dtcrRight.setHorizontalAlignment(SwingConstants.RIGHT);
		
		//컬럼 정보 가져오기
		TableColumnModel cModel = table.getColumnModel();
		
		int columnCount = cModel.getColumnCount();//컬럼의 개수
		//반복문 돌려도 됨
		cModel.getColumn(0).setPreferredWidth(70);//간격
		cModel.getColumn(0).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(1).setPreferredWidth(70);//간격
		cModel.getColumn(1).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(2).setPreferredWidth(70);//간격
		cModel.getColumn(2).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(3).setPreferredWidth(70);//간격
		cModel.getColumn(3).setCellRenderer(dtcrCenter);
		
		cModel.getColumn(4).setPreferredWidth(150);//간격
		cModel.getColumn(4).setCellRenderer(dtcrRight);
		
		
		
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));

		JPanel panel_Top = new JPanel();
		add(panel_Top, BorderLayout.NORTH);

		btnPrn = new JButton("리스트보기");
		btnPrn.addActionListener(this);
		panel_Top.add(btnPrn);

		JPanel panel_bottom = new JPanel();
		add(panel_bottom, BorderLayout.SOUTH);

		final JLabel lblSelected = new JLabel("New label");
		panel_bottom.add(lblSelected);

		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		String[] colNames = { "사원번호", "사원명", "직책", "매니져", "급여", "부서번호" };
		TableModel model = new DefaultTableModel(colNames, 0) {

			@Override
			public Class<?> getColumnClass(int columnIndex) {
				return getValueAt(0, columnIndex).getClass();
			}
		}; // 첫번째 행에 들어갔을때 타입을 지정해준다.
		table = new JTable(model);
		table.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				int row = table.getSelectedRow();// 행의 전체의 행의 개수

				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < table.getColumnCount(); i++) {
					sb.append(table.getModel().getValueAt(row, i) + " ");

				}
				lblSelected.setText(sb.toString());
			}
			
		});
		table.setAutoCreateRowSorter(true);//정렬

		scrollPane.setViewportView(table);

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnPrn) {
			actionPerformedBtnAdd(e);
		}
	}

	protected void actionPerformedBtnAdd(ActionEvent e) {
		
		DecimalFormat df = new DecimalFormat("#,###");
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		// 타입이 각각 틀려서 Object[] 배열 선언
		Object[] newRow = { new Integer((int) (Math.random() * 100) + 1),
				"김태희", "사원", new Integer((int) (Math.random() * 100) + 1),
				df.format(new Integer((int) (Math.random() * 100000) + 1)),
				new Integer((int) (Math.random() * 10) + 1) };

		model.addRow(newRow);
	}
}
